package com.core;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.core.annotation.BindView;
import com.core.annotation.Layout;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;


/**
 * Created by wk on 14-10-26.
 */
public abstract  class BaseActivity extends Activity {
    private Activity childActivity;
    private Bundle mInstanceState;
    private ViewOnclickListener click;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what)
            {

                case 1:
                    onInitView();
                    onLoadView(mInstanceState);
                    break;
            }
        }
    };

    //加载view
    private void onInitView() {
        Field fields[] = childActivity.getClass().getDeclaredFields();

        for (Field field:fields)
        {
            if (field.isAnnotationPresent(BindView.class))
            {
                bindView(field);
            }
        }
    }

    private void bindView(Field field) {
        BindView bindView = field.getAnnotation(BindView.class);
        int id = bindView.id();
        boolean click = bindView.click();
        try
        {
            field.setAccessible(true);
            View view = findViewById(id);
            field.set(childActivity,view);
            if (click)
            {
                Class clazz = field.getType();
                Method method = clazz.getMethod("setOnClickListener", View.OnClickListener.class);
                method.invoke(view,click);
            }

        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    protected void onCreateView(Bundle savedInstanceState,Activity activity) {
        super.onCreate(savedInstanceState);
        childActivity = activity;
        initListener();
        initLayout();
        mInstanceState = savedInstanceState;
        ThreadPools.execute(new DataThread());
    }

    private void initListener() {
        click = new ViewOnclickListener();
    }

    private void initLayout() {
        Annotation []annotations = childActivity.getClass().getDeclaredAnnotations();

        for (Annotation annotation:annotations)
        {
            Class clazz = annotation.annotationType();
            //如果使layout注解
            if ( clazz.equals(Layout.class))
            {
               Layout layout = (Layout) annotation;
               int id =  layout.lay_id();
               setContentView(id);
            }

        }
    }


    protected void viewOnClick(View view){}
    public abstract void onloadData(Bundle savedInstanceState);
    //加载view的内容
    public abstract void onLoadView(Bundle savedInstanceState);
    private class DataThread extends Thread{
        @Override
        public void run() {
            onloadData(mInstanceState);
            handler.sendEmptyMessage(1);
        }
    }
    private class ViewOnclickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            viewOnClick(v);
        }
    }
}
